class UnionFind():
    def __init__(self, num_node):
        self.num_node = num_node
        self.parent = [i for i in range(num_node+1)]
    
    def find(self, node):
        if self.parent[node] != node:
            self.parent[node] = self.find(self.parent[node])
        return self.parent[node]
        
    def union(self, s, t):
        parent_s = self.find(s)
        parent_t = self.find(t)
        if parent_t != parent_s:
            self.parent[parent_t] = parent_s
            
    def is_same(self, s,t):
        return self.find(s) == self.find(t)

def kruskal(edges_value, N):
    edges_value.sort(key = lambda x : x[:][:][2])
    uf = UnionFind(N)
    
    dist = 0
    for s, t, value in edges_value:
        if not uf.is_same(s, t):
            uf.union(s, t)
            dist += value
    
    return dist
        
        

def main():
    N, edge = map(int, input().split())
    
    edges_value = []
    
    for i in range(edge):
        edges_value.append(list(map(int, input().split())))
        
    dist = kruskal(edges_value, N)
    print(dist)
    
    
main()
    